package com.xinke.juc.case04;

import com.xinke.juc.util.Sleeper;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.locks.ReentrantLock;

/**
 * @Author：xinke
 * @Date：2025/10/4
 * @Desc: 锁超时 - 立刻失败
 */
@Slf4j
public class ReentrantLock04 {

    public static void main(String[] args) {
        ReentrantLock lock = new ReentrantLock();

        Thread t1 = new Thread(() -> {
            log.info("启动。。。");
            if (!lock.tryLock()) {
                log.info("获取立刻失败，返回");
                return;
            }
            try {
                log.info("获得了锁");
            } finally {
                lock.unlock();
            }
        }, "t1");

        lock.lock();
        log.info("获得了锁");
        t1.start();
        try {
            Sleeper.sleep(2);
        } finally {
            lock.unlock();
        }

    }
}
